#include "stdint.h"
#include "stdio.h"
#include "sdkconfig.h"
#include "parameters.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "freertos/timers.h"

#include "DevicesQueue.h"
#include "DevicesKey.h"

#include "taskKey.h"


TaskHandle_t g_TaskKeyHand = NULL;   /* 按键消息处理任务句柄 */


void vTaskKey(void *pvParameters)
{
    KeyType typeKey = {0};

    while (1)
    {
        /* 等待任务消息 */
        ulTaskNotifyTake(pdTRUE, portMAX_DELAY);

        /* 获取按键消息 */
        while(enumQueuePopDatas(&g_TypeQueueKeyInput, &typeKey, sizeof(typeKey)) == queueNormal)
        {
            printf("combo: %d, valueDown: %02X, timeDown: %d, valueUp: %02X, timeUp: %d, state: %02X\r\n",
                    (int)typeKey.cntCombo,
                    (int)typeKey.valueDown,
                    (int)typeKey.timeDown,
                    (int)typeKey.valueUp,
                    (int)typeKey.timeUp,
                    (int)typeKey.state);

            /* 短按、并已经松开按键 */
            if(typeKey.state == (keyDown | keyShort))
            {
                if(typeKey.valueUp == KEY_RESET)
                {
                }
            }
            /* 长按、并还没有松开按键 */
            if(typeKey.state == (keyDown | keyLong))
            {
                if(typeKey.valueDown == KEY_RESET)
                {
                    cParametersDeinit();
                    cParametersSave();
                    esp_restart();
                }
            }
        }
    }
}
